define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    // del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    // import_url: 'order/order/import',
                    table: 'play_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),visible:false, operate: false},
                        {field: 'play_order_no', title: '平台单号'},
                        {field: 'shop_name', title: '店铺名称', operate: 'LIKE', width: 100, align: 'left',halign:"center"},
                        {field: 'shop_no', title: '店铺id', operate: 'LIKE', width: 100},
                        // {field: 'is_yunzhan', title: '渠道名称', searchList: Config.channelList},
                        {field: 'platform_no', title: '外卖平台', searchList: {"1":"美团","2":"饿了么","3":"京东"}, formatter: Table.api.formatter.status},
                        {field: 'wm_order_sn', title: '外卖单号', operate: 'LIKE'},
                        {field: 'user_id', title: '用户ID'},
                        {field: 'lng', title: '用户昵称', operate: false},
                        {field: 'amount', title: '第几次下单'},
                        // {field: 'actual_rebate_amt', title: '渠道结算金额', operate: false},
                        // {field: 'proof.deduction_beans', title: '减少渠道结算金额', operate: false},
                        // {field: 'service_charge_amt', title: '服务费', operate: false},
                        // {field: 'scheme_no_text', title: '活动方案', operate: false},
                        {field: 'rebate_amt', title: '商家结算金额', operate: false},
                        {field: 'actual_rebate_amt', title: '用户返现金额', operate: false},
                        {field: 'service_admin_by', title: '所属业务员',searchList: Config.salesmanList},
                        {field: 'is_pay', searchList: {"0":"未结算","1":"已结算"}, title: '是否结算', formatter: Table.api.formatter.status},
                        {field: 'order_status', title: __('Status'), searchList: {"2":"待审核","3":"已完成","-1":"已取消","-3":"已驳回",'-7':'异常订单','-8':'异常订单（客服已处理）','-9':'异常订单（已完成）','-4':'异常订单（已取消）'}, formatter: Table.api.formatter.status},
                        {field: 'proof.one_image_url', title: '订单截图', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'proof.two_image_url', title: '订单截图2', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'proof.three_image_url', title: '评价截图', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'place_time', title: '外卖订单时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'create_dt', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_dt', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', 
                        title: __('Operate'), 
                        table: table, 
                        events: Table.api.events.operate,
                        buttons: [
                            {
                                name: "edit",
                                text: __("订单详情"),
                                classname: "btn btn-xs btn-info btn-dialog",
                                icon: "fa fa-eye",
                                url: "order/order/edit"
                            },
                            {
                                name: "xzxd",
                                title: "手动转账",
                                classname: "btn btn-xs btn-warning btn-magic btn-ajax",
                                icon: "fa",
                                text: "手动转账",
                                confirm: "确认转账？",
                                url: "order/order/pay",
                                visible: function(row){
                                    if (0 == row.is_transfer && row.order_status==3) return ! 0
                                }, 
                                refresh: true
                            },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });
//*************************** 自定义export开始
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("请选择需要导出数据的id<form action='" + Fast.api.fixurl("order/order/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'><input type='number' name='start' class='form-control' placeholder='请输入最小id' style='margin-bottom:10px;'><input type='number' name='end' class='form-control' placeholder='请输入最大id'></form>", {
                    title: '导出数据',
                    //btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    // btn: ["选中项(" + ids.length + "条)"],
                    btn: ["导出"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            //*************************** 自定义export结束
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on("click","#is_limited",function(){
                    var is_limited = $('#is_limited').prop("checked");
                    var word = '';
                    if (is_limited==false) {
                        word = '确定取消用户下单限制？';
                    } else {
                        word = '确定限制用户在该店铺下单？';
                    }
                    if (confirm(word)) {
                        var play_order_no = $('#play_order_no').text();
                        $.ajax({
                            url:"/hFtNsJROTk.php/order/order/limited",
                            data: { is_limited: is_limited,play_order_no:play_order_no },
                            success:function(result){
                                if (result.code==1) {
                                    Layer.alert(result.msg, {title: 'Success', icon: 1});
                                } else {
                                    Layer.alert(result.msg, {title: 'Fail', icon: 0});
                                }
                                return false;
                            }
                        });
                    } else {
                        if (is_limited==false) {
                            $('#is_limited').prop("checked",true);
                        } else {
                            $('#is_limited').prop("checked",false);
                        }
                    }
                });
            }
        }
    };
    return Controller;
});
